package com.ybc.chinese.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.viewpagerindicator.CirclePageIndicator;
import com.ybc.chinese.R;
import com.ybc.chinese.activity.CompositionActivity;
import com.ybc.chinese.activity.IdiomActivity;
import com.ybc.chinese.activity.PoetryActivity;
import com.ybc.chinese.activity.SpellActivity;
import com.ybc.chinese.adapter.AutoScrollPagerAdapter;
import com.ybc.chinese.bean.CarouselFigure;
import com.ybc.chinese.utils.CommonUtil;
import com.ybc.chinese.widget.autoScroll.AutoScrollViewPager;

import java.util.List;

/**
 * <p>首页顶部<p/>
 * Created by Yubch on 17/2/18.
 */
public class HomeTopView extends LinearLayout implements View.OnClickListener {
    private Context mCtx;
    private AutoScrollViewPager vpCarousel;
    private CirclePageIndicator mIndicator;
    private AutoScrollPagerAdapter mAutoScrollPagerAdapter;

    public HomeTopView(Context context) {
        super(context);
        initView(context);
    }

    public HomeTopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public HomeTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mCtx = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_home_top, this);
        vpCarousel = (AutoScrollViewPager) view.findViewById(R.id.vp_carousel);
        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        vpCarousel.setOffscreenPageLimit(10);
        mAutoScrollPagerAdapter = new AutoScrollPagerAdapter(mCtx, vpCarousel, mIndicator);
        vpCarousel.setAdapter(mAutoScrollPagerAdapter);
        mIndicator.setViewPager(vpCarousel);
        mIndicator.setRadius(10);
        mIndicator.setFillColor(getResources().getColor(R.color.app_color));
        mIndicator.setStrokeColor(getResources().getColor(R.color.white));
        TextView tvSpell = (TextView) view.findViewById(R.id.tv_spell);
        tvSpell.setOnClickListener(this);
        TextView tvPoetry = (TextView) view.findViewById(R.id.tv_poetry);
        tvPoetry.setOnClickListener(this);
        TextView tvComposition = (TextView) view.findViewById(R.id.tv_composition);
        tvComposition.setOnClickListener(this);
        TextView tvIdiom = (TextView) view.findViewById(R.id.tv_idiom);
        tvIdiom.setOnClickListener(this);
    }

    public void setData(List<CarouselFigure> carouselFigures) {
        if (!CommonUtil.isEmpty(carouselFigures)) {
            mAutoScrollPagerAdapter.setDataList(carouselFigures);
            mAutoScrollPagerAdapter.notifyDataSetChanged();
        }
    }

    public AutoScrollViewPager getVpCarousel() {
        return vpCarousel;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_spell: //拼音
                Intent intent = new Intent(mCtx, SpellActivity.class);
                mCtx.startActivity(intent);
                break;
            case R.id.tv_poetry: //古诗
                intent = new Intent(mCtx, PoetryActivity.class);
                mCtx.startActivity(intent);
                break;
            case R.id.tv_composition: //作文
                intent = new Intent(mCtx, CompositionActivity.class);
                mCtx.startActivity(intent);
                break;
            case R.id.tv_idiom: //成语
                intent = new Intent(mCtx, IdiomActivity.class);
                mCtx.startActivity(intent);
        }
    }
}
